package cn.tedu.tmall.front.mall.dao.repository.impl;

import cn.tedu.tmall.front.mall.dao.mapper.CategoryMapper;
import cn.tedu.tmall.front.mall.dao.repository.ICategoryRepository;
import cn.tedu.tmall.front.mall.pojo.po.CategoryPO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class CategoryRepository implements ICategoryRepository {

    @Autowired
    private CategoryMapper categoryMapper;


    @Override
    public CategoryPO getCategoryById(Long id) {
        return categoryMapper.selectById(id);
    }
    @Override
    public List<CategoryPO> getAll() {
        QueryWrapper queryWrapper = new QueryWrapper();
        //queryWrapper.eq("1",1);
        //select from table where 1 = 1
        return categoryMapper.selectList(queryWrapper);
    }

    @Override
    public List<CategoryPO> getTopCategory() {
        return getChildrenCategoryByParentId(0L);
    }

    @Override
    public List<CategoryPO> getChildrenCategory(Long parentId) {
        return getChildrenCategoryByParentId(parentId);
    }

    private List<CategoryPO> getChildrenCategoryByParentId(Long parentId) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("parent_id",parentId);
        return categoryMapper.selectList(queryWrapper);
    }
}
